package com.huatai.bi.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @program: ganghua
 * @description: 覆盖公共的配置
 * @author: songxiude
 * @create: 2024-08-05 17:35
 **/
@Configuration
public class DateConfig {

	@Bean
	@Primary
	@Order(Ordered.HIGHEST_PRECEDENCE)
	public MetaObjectHandler metaObjectHandler() {
		return new MetaObjectHandler() {
			public void insertFill(MetaObject metaObject) {
				this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
				this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
			}

			@Override
			public void updateFill(MetaObject metaObject) {
				//this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
				this.setFieldValByName("updateTime", new Date(), metaObject);
			}
		};
	}
}
